﻿using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace CPPEI.Coolzon.BrowserApp
{
    public class StartupUtil
    {
        /// <summary>
        /// 获取启动参数
        /// </summary>
        /// <returns></returns>
        public static string GetStartupParamString()
        {
            if (BrowserInteropHelper.IsBrowserHosted)
            {
                // 1.从页面上获取启动参数
                dynamic scriptObject = BrowserInteropHelper.HostScript;
                if (scriptObject != null)
                {
                    //获取已经加密的配置信息
                    return scriptObject.GetConfigInfos();
                }
            }

            // 2.从地址上获取
            string startupUrl = App.Current.StartupUri.ToString();
            string[] urlArr = startupUrl.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
            if (urlArr.Length == 2)
            {
                return urlArr[1];
            }
            return null;
        }

        /// <summary>
        /// 解析配置参数
        /// </summary>
        /// <param name="paramString"></param>
        public static void ParseParamString(string paramString)
        {
            if (!string.IsNullOrEmpty(paramString))
            {
                //解密配置信息
                string decryptString = CryptoHelper.MD5Decrypt(paramString);

                string[] paramArr = decryptString.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < paramArr.Length; index++)
                {
                    string[] kvArr = paramArr[index].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    string paramName = kvArr[0].ToUpper();
                    if (paramName == "HOST")
                    {
                        CZEnvironment.HostUrl = kvArr[1];
                    }
                    else if (paramName == "USERIDENTITY")
                    {
                        CZEnvironment.UserIdentity = kvArr[1];
                    }
                    else if (paramName == "SOLUTION")
                    {
                        CZEnvironment.Solution = kvArr[1];
                    }
                    else if (paramName == "ACCOUNT")
                    {
                        CZEnvironment.UserAccount = kvArr[1];
                    }
                    else if (paramName == "PASSWORD")
                    {
                        CZEnvironment.UserPassword = kvArr[1];
                    }
                }
            }
            else
            {
                CZEnvironment.HostUrl = "Http://localhost:2033/";
                CZEnvironment.UserIdentity = "";
                CZEnvironment.Solution = "CPPEI_DESKTOP_20150105";
                CZEnvironment.UserAccount = "cool";
                CZEnvironment.UserPassword = "1";
            }
        }
    }
}
